package org.yf.samples.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yf.samples.dao.IExamRoleDAO;
import org.yf.samples.dao.IExamUserRoleDAO;
import org.yf.samples.dao.IUserDAO;
import org.yf.samples.model.ExamRole;
import org.yf.samples.model.ExamUserRole;
import org.yf.samples.model.ExamUserVO;
import org.yf.samples.model.User;
import org.yf.samples.util.ExcelUtils;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

	@Autowired
	private IUserDAO userDAO;
	
	@Autowired
	private IExamUserRoleDAO examUserRoleDAO;
	
	@Autowired
	private IExamRoleDAO examRoleDAO;
	
	@Override
	public List<ExamUserVO> findAllUsers() {
		List<User> userList = userDAO.FindAllUsers();
		List<ExamUserVO> voList = new ArrayList<ExamUserVO>();
		for(User user : userList){
			ExamUserVO vo = new ExamUserVO();
			BeanUtils.copyProperties(user, vo);
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public Integer createUser(ExamUserVO userVO) {
		User user = new User();
		BeanUtils.copyProperties(userVO, user);
		try {
			userDAO.createUser(user);
			ExamRole role = new ExamRole(25, "学生");
			examUserRoleDAO.createUserRole(new ExamUserRole(user, role));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	@Override
	public Integer deleteUser(String userId) {
		try {
			userDAO.deleteUser(userId);
			examUserRoleDAO.deleteUserRole(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	@Override
	public ExamUserVO getUserById(String userId) throws Exception {
		User user = userDAO.getUserById(userId);
		ExamUserVO vo = new ExamUserVO();
		BeanUtils.copyProperties(user, vo);
		String role = examUserRoleDAO.getExamUserRoleByUserId(userId).getExamRole().getName();
		int userRole = 0;
		switch(role){
			case "学生":
				userRole = 25;
				break;
			case "教师":
				userRole = 26;
				break;
			case "管理员":
				userRole = 27;
				break;
		}
		vo.setUserRole(userRole);
		return vo;
	}

	@Override
	public Integer editUser(ExamUserVO userVO) throws Exception {
		User user = new User();
		BeanUtils.copyProperties(userVO, user);
		userDAO.editUser(user);
		return 1;
	}

	@Override
	public void saveStudentByExcel(InputStream inputStream, String postfix) {
		Workbook wk = ExcelUtils.openWorkbook(inputStream, postfix);
		List<ExamUserVO> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<User> entities = new ArrayList<User>();
		List<ExamUserRole> examUserRoleList = new ArrayList<ExamUserRole>();
		if(allExcelUser != null){
			ExamRole role = examRoleDAO.getExamRoleByName("学生");
			User entity = null;
			for(int i = 0; i < allExcelUser.size(); i++){
				entity = new User();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
				entity.setLastLockTime(new java.sql.Date(System.currentTimeMillis()));
				entities.add(entity);
				examUserRoleList.add(new ExamUserRole(entity, role));
			}
		}
		userDAO.saveUser(entities);
		examUserRoleDAO.saveExamUserRoles(examUserRoleList);
 	}

	@Override
	public List<ExamUserVO> findAllStudents() {
		List<User> userList = examUserRoleDAO.findAllStudents();
		List<ExamUserVO> voList = new ArrayList<ExamUserVO>();
		for(User user : userList){
			ExamUserVO vo = new ExamUserVO();
			BeanUtils.copyProperties(user, vo);
			voList.add(vo);
		}
		return voList;
	}
	
}
